<?php
class DBManager {
    private static $instance = null;
    private $connection = null;
    
    private function __construct() {
        // 私有构造函数，防止外部实例化
    }
    
    public static function getInstance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    public function getConnection() {
        if ($this->connection === null) {
            require_once 'config.php';
            $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            
            if ($this->connection->connect_error) {
                die("数据库连接失败: " . $this->connection->connect_error);
            }
            
            $this->connection->set_charset("utf8mb4");
        }
        return $this->connection;
    }
    
    public function closeConnection() {
        if ($this->connection !== null) {
            $this->connection->close();
            $this->connection = null;
        }
    }
    
    // 防止克隆对象
    private function __clone() {}
    
    // 防止反序列化
    private function __wakeup() {}
} 